package org.orz.cloud.common.result;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ResultPageDTO<T> {

    /**
     * 是否成功 true成功 false失败
     */
    private Boolean success;

    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 当前页码
     */
    private Integer pageNum;

    /**
     * 每页数据条数
     */
    private Integer pageSize;

    /**
     * 总页数
     */
    private Integer pageCount;

    /**
     * 数据总量
     */
    private Long total;

    /**
     * 响应参数
     */
    private List<T> data;

    public static <T> ResultPageDTO<T> restPage(List<T> dataList){
        ResultPageDTO<T> result = new ResultPageDTO<>();
        PageInfo<T> pageInfo = new PageInfo<>(dataList);
        result.setSuccess(Boolean.TRUE);
        result.setCode("0");
        result.setPageCount(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }


    public static <R, T> ResultPageDTO<T> restPage(List<R> dataList, Class<T> targetClass){
        ResultPageDTO<T> result = new ResultPageDTO<>();
        PageInfo<R> pageInfo = new PageInfo<>(dataList);
        result.setSuccess(Boolean.TRUE);
        result.setCode("0");
        result.setPageCount(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        List<T> data = new ArrayList<>();
        for (R item : dataList) {
            data.add(BeanUtil.toBean(item, targetClass));
        }
        result.setData(data);
        return result;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageCount() {
        return pageCount;
    }

    public void setPageCount(Integer pageCount) {
        this.pageCount = pageCount;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
